import calendar
from datetime import datetime
import decimal
import uuid
from pylibs import SqlHelper
import os,json
import argparse

import logging  
logging.basicConfig(level=logging.INFO)  

class GlobalObject:
    statistArray= [
        {"numb":1, "name":"基金收支情况 - 居民医保统筹基金分类支出进度表（仪表图）"},
        {"numb":2, "name":"基金收支情况 - 居民医保统筹基金支出进度表（柱状图+折线图）"},
        {"numb":3, "name":"基金收支情况 - 居民医保统筹基金支出情况表（按区域）（饼图）"},
        {"numb":4, "name":"基金收支情况 - 居民医保就医地分布（地图）"},
        {"numb":21, "name":"运行指标 - 城乡居民(门诊/住院)支出进度表(堆叠柱状图)"},
        {"numb":31, "name":"评价指标 - xxxx年xx月份(居民/职工)住院综合评价（雷达图)"},
        {"numb":33, "name":"评价指标 - xxxx年xx月份居民住院综合评价(雷达图)"},
        {"numb":34, "name":"评价指标 - xxxx年xx月份居民住院综合评价(雷达图)"},
        {"numb":35, "name":"评价指标 - xxxx年xx月份就诊病种(词云)"},

        {"numb":41, "name":"考核指标 - xxxx年xx月份城乡居民基本医疗保险门诊统筹就诊情况"},
        {"numb":42, "name":"考核指标 - xxxx年xx月份城乡居民基本医疗保险住院评价指标"},

        {"numb":51, "name":"考核指标 - xxxx年xx月份城乡居民基本医疗保险住院评价指标"},

        {"numb":1001, "name":"计算月度预算值(t_month_zongyusuan)"},
        
    ]

    def getStatistName( numb):
        name =''
        for obj1 in GlobalObject.statistArray:
            if obj1['numb'] == numb :
                name = obj1['name']
                break
        return name


def initSqlHelper():

    parser = argparse.ArgumentParser(description='ybtool1 参数')  
  
    # 添加一个命令行参数 -c 或 --config，类型为字符串，默认值为 None  
    parser.add_argument('-c', '--config', type=str, help='配置文件信息')  
  
    # 解析命令行参数  
    args = parser.parse_args()  
    
    if args.config is not None:
        configJsonFile = args.config
    else:
        configJsonFile = 'config.json'
    
    if os.path.exists(configJsonFile):  
        # 如果 config_json_file 存在，则加载到 configJson 中  
        with open(configJsonFile, 'r', encoding='utf-8') as file1:  
            configJson = json.load(file1)  

    SqlHelper.dbinfo['host']= configJson['db_host']
    SqlHelper.dbinfo['port']= configJson['db_port']
    SqlHelper.dbinfo['user']= configJson['db_user']
    SqlHelper.dbinfo['passwd']= configJson['db_passwd']
    SqlHelper.dbinfo['db']= configJson['db_database']

# 该方法刷新数据表 t_statist_01， 对应 《运行分析表.xlsx》的 基金收支情况 - 居民医保统筹基金分类支出进度表（仪表图）
def refreshStatist01(year,month):  

    if year is None:
        logging.warn("参数year is None，刷新被暂停")
        return {"code":501, "msg":"参数year is None，刷新被暂停"}
    
    if month is None:
       logging.warn("参数 month is None，刷新被暂停")
       return {"code":503, "msg":"参数 month is None，刷新被暂停"}
    
    # 刷新 县内住院的数据

    arrLeixing =["县内住院","市内住院","市外住院","门诊统筹","门诊慢性病","意外伤害住院"]
    arrSubWhere = [" and insutype='390'  and insuAdmdvs='130534' ",
                   " and insutype='390' and shi_code = '1305' and xian_code != '13054' and insuAdmdvs='130534' and medType='21' " ,
                   " and insutype='390' and shi_code != '1305' and insuAdmdvs='130534' and medType='21' ",
                   " and insutype='390' and insuAdmdvs='130534' and medType='11' ",
                   " and insutype='390' and insuAdmdvs='130534' and medType='14' ",
                   " and insutype='390' and insuAdmdvs='130534' and medType='22' "
                   ]

    ## 获取 县住院的预算值
    for i9 in range(len(arrLeixing)):
        leixing = arrLeixing[i9]
        subWhere = arrSubWhere[i9]


        xnzyYusuan = _getYearYusuan(year, leixing)

        ## 获取 县内住院的当月支出金额 
        dangyueStartTime, dangyueEndTime = _getMonthStartAndEndTime(year, month )
        xnzyDangyueZhichu = _statistZhichu(dangyueStartTime, dangyueEndTime, subWhere)

        ## 获取 县内住院的往月累计支出金额
        yearStartTime = datetime(year,1,1)
        xnzyWangyueZhichu = _statistZhichu(yearStartTime, dangyueStartTime, subWhere )

        ## 计算 县内住院 当月支出进度
        objXnzyDangyueJindu = _calcJindu(xnzyDangyueZhichu, xnzyYusuan)

        ## 计算 县内住院 往月累计支出进度 
        objXnzyWangyueJindu = _calcJindu(xnzyWangyueZhichu, xnzyYusuan)

        ## 计算 县内住院 预计支出进度
        objXnzyYujiJindu = _calcJindu(month,12)

        ## 计算 县内住院 预计支出金额
        xnzyYujiZhichu = xnzyYusuan * month / 12

        ### 保存 县内住院 信息到 t_statist1
        _saveStatist01( year,month, leixing, xnzyYusuan,
                        xnzyDangyueZhichu, xnzyWangyueZhichu, xnzyYujiZhichu,
                        objXnzyDangyueJindu['val2'],objXnzyWangyueJindu['val2'], 
                        objXnzyYujiJindu['val2'], i9+1)
    
    

    return {"code":0, "msg":"success"  }



# 该方法刷新数据表 t_statist_02， 对应 《运行分析表.xlsx》的 基金收支情况 - 居民医保统筹基金支出进度表（柱状图+折线图）
def refreshStatist02(year):  

    # 获取 年度 总共的预算
    sql9="select sum(yusuan_money) as zongyusuan from t_month_zongyusuan where year_val= %(year_val)s  "
    param9 = {"year_val": year }
    res9 = SqlHelper.fetch_one(sql9, param9)
    zongyusuan = decimal.Decimal('0')
    if res9 is not None:
        zongyusuan = res9['zongyusuan']

    # 循环12 个月， 计算每个月的 总预算、支出总额、支出完成率、累计支出率
    leijizhichu = 0
    for i1 in range(12):
        month1 = i1+1

        ### 获取 月度 预算
        monthYusuan = _getMonthZongYusuan(year, month1 )

        startTime,endTime = _getMonthStartAndEndTime(year, month1)

        ### 获取当月支出金额
        monthZhichu = _statistZhichu(startTime,endTime, "")

        ### 计算累计支出率
        leijizhichu += monthZhichu
        objMonthLeijiZhichulv = _calcJindu(leijizhichu, zongyusuan)
        monthLeijiZhichilv = objMonthLeijiZhichulv['val2']

        ### 计算支出完成率
        objZhichuWanchenglv = _calcJindu(monthZhichu, zongyusuan)
        monthZhichuWanchenglv = round( objZhichuWanchenglv['val2'] - ( decimal.Decimal("1.0")/ decimal.Decimal("12.0") ) , 4 )

        _saveStatist02(year,month1, monthYusuan, monthZhichu, monthZhichuWanchenglv , monthLeijiZhichilv , month1) 


    return {"code":0,"msg":"success" }


# 该方法刷新数据表 t_statist_03， 对应 《运行分析表.xlsx》的 基金收支情况 - 居民医保统筹基金支出情况表（按区域）（饼图）
def refreshStatist03(year):  

    ## 定义区域数组
    arrArea=[{"area":"县内","subWhere":" and insutype='390' and insuAdmdvs='130534' and xian_code='130534' "},
             {"area":"市内","subWhere":" and insutype='390' and insuAdmdvs='130534' and shi_code= '1305' and xian_code!='130534' "},
             {"area":"省内","subWhere":" and insutype='390' and insuAdmdvs='130534' and sheng_code='13' and shi_code!='1305' "},
             {"area":"省外","subWhere":" and insutype='390' and insuAdmdvs='130534' and sheng_code!='13' "},
             ]

    jinnian = year
    qunian = year -1

    dangnianStartTime = datetime(jinnian,1,1)
    dangnianEndTime = datetime(jinnian+1,1,1)

    qunianStartTime = datetime(qunian,1,1)
    qunianEndTime = datetime(qunian+1,1,1)

    ## 获取总金额
    dangnianZongZhichu = _statistZhichu(dangnianStartTime,dangnianEndTime, "")
    qunianZongZhichu = _statistZhichu(qunianStartTime,qunianEndTime, "")

    ## 获取各区域支出
    for i1 in range(len(arrArea)):
        area = arrArea[i1]['area']
        subWhere = arrArea[i1]['subWhere']

        ## 获取该条件下 去年和今年累计支出
        areaQunianZongZhichu = _statistZhichu(qunianStartTime,qunianEndTime, subWhere)
        areaDangnianZongZhichu = _statistZhichu(dangnianStartTime,dangnianEndTime, subWhere)
        
        objAreaQunianJindu = _calcJindu( areaQunianZongZhichu, qunianZongZhichu )
        objAreaDangnianJindu = _calcJindu(areaDangnianZongZhichu, dangnianZongZhichu )

        _saveStatist03(jinnian, area, 
                       qunianLeijiMoney=areaQunianZongZhichu,
                       qunianLeijiBili= objAreaQunianJindu['val2'],
                       dangnianLeijiMoney= areaDangnianZongZhichu,
                       dangnianLeijiBili= objAreaDangnianJindu['val2'],
                       sortNo= i1+1 )

    return {"code":0,"msg":"success"}



# 该方法刷新数据表 t_statist_04， 对应 《运行分析表.xlsx》的 基金收支情况 - 居民医保就医地分布（地图）
def refreshStatist04(year):  

    dangnianStartTime = datetime(year,1,1)
    dangnianEndTime = datetime(year+1,1,1)
    
    # 获取各省份的交易总额
    sql9 = "select sheng_code,sheng_name, count(1) as zong_renci, sum(hifpPay) as zong_zhichu from t_ybtool1_jiesuan_mingxi \
        where  setlTime between %(startSetlTime)s and %(endSetlTime)s and xian_code !='130534'  \
        group by sheng_code, sheng_name order by sheng_code"
    param9 = {"startSetlTime": dangnianStartTime,"endSetlTime":dangnianEndTime}
    res9 = SqlHelper.fetch_all(sql9, param9)

    # 加载所有省的json
    with open('./sheng.json',encoding='utf-8') as fsheng:
        srcShengJson=json.load(fsheng)
        
        for objSheng in srcShengJson:
            shengCode=objSheng['code']
            shengName = objSheng['name']

            hasCurrSheng = False
            for row7 in res9:
                if row7['sheng_code']==shengCode:
                    hasCurrSheng = True
                    break

            if not hasCurrSheng:
                res9.append( {"sheng_code":shengCode, "sheng_name": shengName , "zong_renci": decimal.Decimal("0.0") , "zong_zhichu": decimal.Decimal("0.0") } )
    
    # 将数据循环保存到 statist04 中
    if res9 is not None:
        for i8 in range(len(res9)):
            row8 = res9[i8]

            shengCode = row8['sheng_code']
            shengName = row8['sheng_name']
            zongRenci = row8['zong_renci']
            zongZhichu = row8['zong_zhichu']

            sql8 = "insert into t_statist_04 (id,year_val,sheng_code,sheng_name,leiji_renci,leiji_money,sort_no) \
                values (%(id)s,%(year_val)s,%(sheng_code)s,%(sheng_name)s,%(leiji_renci)s,%(leiji_money)s,%(sort_no)s) \
                ON DUPLICATE KEY UPDATE \
                sheng_name=VALUES(sheng_name),\
                leiji_renci=VALUES(leiji_renci),\
                leiji_money=VALUES(leiji_money),\
                sort_no=VALUES(sort_no)\
          "
            param8 = {"id": uuid.uuid4().hex, "year_val": year, 
                      "sheng_code": shengCode, "sheng_name":shengName,
                      "leiji_renci":zongRenci,
                      "leiji_money": zongZhichu, "sort_no": i8+1}
            SqlHelper.insert(sql8,param8)

    return {"code":0,"msg":"success"}

# 该方法刷新数据表 t_statist_21， 对应 《运行分析表.xlsx》的 运行指标 - 城乡居民(门诊/住院)支出进度表(堆叠柱状图)
# zhichuType取值 1 门诊 2 住院
def refreshStatist21(zhichuType, year, month):  
    dangyear = year 
    dangmonth = month

    dangnianStartTime = datetime(year,1,1)
    shangyueEndTime = datetime(year, month,1)
    
    dangyueStartTime = datetime(year,month,1)
    if month == 12:
        dangyueEndTime = datetime(year+1,1,1)
    else:
        dangyueEndTime = datetime(year,month+1,1)

    # 获取 定点医院的年预算 
    lstYearYusuan = _listQingheDdyyYearYusuan(zhichuType=zhichuType, year=year)

    # 获取所有定点医院
    lstDdyy = _listQingheDdyy()

    # 组装 最终数据
    lst1 = []
    for objyy in lstYearYusuan:
        obj2 = {
            'year_val': year,
            'month_val': month,
            'zhichu_type': zhichuType,
            'fixmedins_code': objyy['fixmedins_code'] ,
            'fixmedins_name': '',
            'year_yusuan': objyy['yusuan'],
            'curr_month_zhichu': 0.0,
            'leiji_zhichu': 0.0,
            'curr_month_zhichu_jindu': 0.0,
            'leiji_zhichu_jindu': 0.0,
            'yuji_zhichu': 0.0,
            'yuji_zhichu_jindu': 0.0,
            'sort_no': objyy['sort_no']
        }

        # 刷新 fixmedins_name 字段
        ddyy = _getQingheDdyyByCode(lst=lstDdyy, fixmedinsCode= obj2['fixmedins_code']) 
        if ddyy is not None:
            obj2['fixmedins_name'] = ddyy['fixmedins_name']

        # 刷新 curr_month_zhichu 
        subWhere = f" and fixmedinsCode = '{obj2['fixmedins_code']}'  and insutype = '390' \
                and insuAdmdvs = '130534'"
        if zhichuType == 1:
            subWhere += f" and medType='11' "
        elif zhichuType ==2:
            subWhere += f" and medType='21' "
        
        currMonthZhichu = _statistZhichu(dangyueStartTime, dangyueEndTime, subWhere)
        leijiZhichu = _statistZhichu(dangnianStartTime, dangyueStartTime, subWhere )

        obj2['curr_month_zhichu'] = currMonthZhichu
        obj2['leiji_zhichu'] = leijiZhichu

        # 处理 当月支出进度
        objDyzcJindu = _calcJindu(obj2['curr_month_zhichu'], obj2['year_yusuan'])
        obj2['curr_month_zhichu_jindu'] = objDyzcJindu['val2']

        # 处理 累计支出进度
        objLjzcJindu = _calcJindu(obj2['leiji_zhichu'], obj2['year_yusuan'])
        obj2['leiji_zhichu_jindu'] = objLjzcJindu['val2']

        # 处理 预计支出金额 
        shengyuMonth = 12 - month 
        yjzcje = obj2['leiji_zhichu'] + obj2['curr_month_zhichu'] * (shengyuMonth + 1)
        yjzczjd = _calcJindu(yjzcje, obj2['year_yusuan'])

        obj2['yuji_zhichu'] = yjzcje
        obj2['yuji_zhichu_jindu'] = yjzczjd['val2']

        lst1.append(obj2)
        
    
    # 保存 lst1 到 t_statis_21 表
    _saveStatist21(lst1)


# 该方法刷新数据表 t_statist_31， 
# 对应 《运行分析表.xlsx》的 
# 评价指标 - 2024年？月份居民住院综合评价（雷达图) 例如  insuType=390  year=2024  month=7
# 评价指标 - 2024年？月份职工住院综合评价（雷达图) 例如  insuType=310  year=2024  month=7
# insuType取值 390 城市居民 310 职工
def refreshStatist31(insuType, year, month):  

    dangyueStartTime = datetime(year,month,1)
    if month == 12:
        dangyueEndTime = datetime(year+1,1,1)
    else:
        dangyueEndTime = datetime(year,month+1,1)

    # 获取 二级医院清单
    sql9="select * from t_qinghe_ddyy where jibie=2 order by sort_no"
    param9 = {}
    resDdyy = SqlHelper.fetch_all(sql9, param9)

    lst1 = []
    # 遍历 二级定点 医院，组装 lst1 
    for objyy in resDdyy:
        obj2 = {
            'year_val': year,
            'month_val': month,
            'insu_type': insuType,
            'fixmedins_code': objyy['fixmedins_code'] ,
            'fixmedins_name': objyy['fixmedins_name'] ,

            'cijun_feiyong': 0.0,
            'pingjun_zhuyuan_tianshu': 0.0,
            'rijun_feiyong': 0.0,
            'shiji_baoxiaobi': 0.0,
            'renci_rentoubi': 0.0,
            'yidi_zhanbi': 0.0,
            'sort_no': objyy['sort_no']
        }

        sum_hiAgreSumfee = decimal.Decimal('0')
        sum_hifpPay =decimal.Decimal('0')
        sumDays = decimal.Decimal('0')
        cnt_psnNo = decimal.Decimal('0')
        cnt_renci = decimal.Decimal('0')
        cnt_130534 = decimal.Decimal('0')

        sqlFrom = " from t_ybtool1_jiesuan_mingxi where setlTime between %(startSetlTime)s and %(endSetlTime)s\
             and fixmedinsCode = %(fixmedinsCode)s and insutype =%(insutype)s and medType = %(medType)s"
        paramFrom = {'startSetlTime':dangyueStartTime,'endSetlTime': dangyueEndTime,
                     'fixmedinsCode': obj2['fixmedins_code'],'insutype': insuType ,'medType':'21',}
        # 获取 sum_hiAgreSumfee   sum_hifpPay  sumDays
        sql8 = f" select fixmedinsCode, sum(hiAgreSumfee) as sum_hiAgreSumfee,  \
            sum(hifpPay) as sum_hifpPay, sum( datediff(enddate,begndate) +1 ) as sumDays \
                  {sqlFrom} group by fixmedinsCode"
        res8 = SqlHelper.fetch_one(sql=sql8,args=paramFrom)
        
        if res8 is not None:
            sum_hiAgreSumfee = res8['sum_hiAgreSumfee']
            sum_hifpPay = res8['sum_hifpPay']
            sumDays = res8['sumDays']

        # 获取 cnt_psnNo 
        sql7 = f"select count( distinct psnNo) as cnt_psnNo {sqlFrom} "
        res7 = SqlHelper.fetch_one(sql=sql7,args=paramFrom)
        if res7 is not None:
            cnt_psnNo = res7['cnt_psnNo']

        # 获取 cnt_renci
        sql6 = "select count(1) as cnt_renci from t_ybtool1_jiesuan_mingxi where setlTime between %(startSetlTime)s and %(endSetlTime)s\
             and fixmedinsCode = %(fixmedinsCode)s group by fixmedinsCode"
        res6 = SqlHelper.fetch_one(sql=sql6,args=paramFrom)
        if res6 is not None:
            cnt_renci = res6['cnt_renci']

        # 获取 cnt_130534
        sql5 = "select count(1) as cnt_130534 from t_ybtool1_jiesuan_mingxi \
            where setlTime between %(startSetlTime)s and %(endSetlTime)s\
             and insuAdmdvs = '130534' "
        res5 = SqlHelper.fetch_one(sql=sql5,args=paramFrom)
        if res5 is not None:
            cnt_130534 = res5['cnt_130534']

        # 计算 cijun_feiyong  
        cijun_feiyong = 10.0
        if cnt_renci != 0:
            cijun_feiyong = round(10 - sum_hiAgreSumfee/cnt_renci/decimal.Decimal('1000'), 1)
        obj2['cijun_feiyong'] = cijun_feiyong

        #计算 pingjun_zhuyuan_tianshu
        pingjun_zhuyuan_tianshu = 15.0 
        if cnt_renci !=0:
            pingjun_zhuyuan_tianshu = 15-round( sumDays/cnt_renci , 1)
        obj2['pingjun_zhuyuan_tianshu'] = pingjun_zhuyuan_tianshu

        # 计算 rijun_feiyong
        rijun_feiyong = 10.0
        if sumDays!=0:
            rijun_feiyong = round(10- sum_hiAgreSumfee/sumDays/ decimal.Decimal('100'),1)
        obj2['rijun_feiyong'] = rijun_feiyong

        # 计算 shiji_baoxiaobi
        shiji_baoxiaobi = 0.0
        if sum_hiAgreSumfee!=0:
            shiji_baoxiaobi = round( sum_hifpPay/sum_hiAgreSumfee* decimal.Decimal('10') ,1)
        obj2['shiji_baoxiaobi'] = shiji_baoxiaobi

        # 计算 renci_rentoubi 
        renci_rentoubi = 11.0
        if cnt_psnNo !=0:
            renci_rentoubi = round( 11 - cnt_renci/cnt_psnNo , 1)
        obj2['renci_rentoubi'] = renci_rentoubi

        # 计算 yidi_zhanbi
        yidi_zhanbi = 10.0
        if cnt_renci !=0:
            yidi_zhanbi = round(10 - cnt_130534/cnt_renci* 10 ,1)
        obj2['yidi_zhanbi'] = yidi_zhanbi

        lst1.append(obj2)


    _saveStatist31(lst1)


# 该方法刷新数据表 t_statist_33， 
# 对应 《运行分析表.xlsx》的 第3个表
# 评价指标 - 2024年？月份居民住院综合评价（雷达图) 例如 医院jibie=1  year=2024  month=7
def refreshStatist33( year, month):  

    dangyueStartTime = datetime(year,month,1)
    if month == 12:
        dangyueEndTime = datetime(year+1,1,1)
    else:
        dangyueEndTime = datetime(year,month+1,1)

    # 获取 一级医院清单
    sql9="select * from t_qinghe_ddyy where jibie=1 order by sort_no"
    param9 = {}
    resDdyy = SqlHelper.fetch_all(sql9, param9)

    lst1 = []
    # 遍历 二级定点 医院，组装 lst1 
    for objyy in resDdyy:
        obj2 = {
            'year_val': year,
            'month_val': month,
            'fixmedins_code': objyy['fixmedins_code'] ,
            'fixmedins_name': objyy['fixmedins_name'] ,

            'cijun_feiyong': 0.0,
            'pingjun_zhuyuan_tianshu': 0.0,
            'rijun_feiyong': 0.0,
            'renci_rentoubi': 0.0,
            'shiji_baoxiaobi': 0.0,
            'sort_no': objyy['sort_no']
        }

        sum_hiAgreSumfee = decimal.Decimal('0')
        sum_hifpPay =decimal.Decimal('0')
        sumDays = decimal.Decimal('0')
        cnt_psnNo = decimal.Decimal('0')
        cnt_renci = decimal.Decimal('0')

        sqlFrom = " from t_ybtool1_jiesuan_mingxi where setlTime between %(startSetlTime)s and %(endSetlTime)s\
             and fixmedinsCode = %(fixmedinsCode)s and insutype =%(insutype)s and medType = %(medType)s"
        paramFrom = {'startSetlTime':dangyueStartTime,'endSetlTime': dangyueEndTime,
                     'fixmedinsCode': obj2['fixmedins_code'],'insutype': '390' ,'medType':'21',}
        # 获取 sum_hiAgreSumfee   sum_hifpPay  sumDays
        sql8 = f" select fixmedinsCode, sum(hiAgreSumfee) as sum_hiAgreSumfee,  \
            sum(hifpPay) as sum_hifpPay, sum( datediff(enddate,begndate) +1 ) as sumDays \
                  {sqlFrom} group by fixmedinsCode"
        res8 = SqlHelper.fetch_one(sql=sql8,args=paramFrom)
        
        if res8 is not None:
            sum_hiAgreSumfee = res8['sum_hiAgreSumfee']
            sum_hifpPay = res8['sum_hifpPay']
            sumDays = res8['sumDays']

        # 获取 cnt_psnNo 
        sql7 = f"select count( distinct psnNo) as cnt_psnNo {sqlFrom} "
        res7 = SqlHelper.fetch_one(sql=sql7,args=paramFrom)
        if res7 is not None:
            cnt_psnNo = res7['cnt_psnNo']

        # 获取 cnt_renci
        sql6 = "select count(1) as cnt_renci from t_ybtool1_jiesuan_mingxi where setlTime between %(startSetlTime)s and %(endSetlTime)s\
             and fixmedinsCode = %(fixmedinsCode)s group by fixmedinsCode"
        res6 = SqlHelper.fetch_one(sql=sql6,args=paramFrom)
        if res6 is not None:
            cnt_renci = res6['cnt_renci']

        # 计算 cijun_feiyong  
        cijun_feiyong = 10.0
        if cnt_renci != 0:
            cijun_feiyong = round(10 - sum_hiAgreSumfee/cnt_renci/decimal.Decimal('1000'), 1)
        obj2['cijun_feiyong'] = cijun_feiyong

        #计算 pingjun_zhuyuan_tianshu
        pingjun_zhuyuan_tianshu = 15.0 
        if cnt_renci !=0:
            pingjun_zhuyuan_tianshu = 15-round( sumDays/cnt_renci , 1)
        obj2['pingjun_zhuyuan_tianshu'] = pingjun_zhuyuan_tianshu

        # 计算 rijun_feiyong
        rijun_feiyong = 10.0
        if sumDays!=0:
            rijun_feiyong = round(10- sum_hiAgreSumfee/sumDays/ decimal.Decimal('100'),1)
        obj2['rijun_feiyong'] = rijun_feiyong

        # 计算 shiji_baoxiaobi
        shiji_baoxiaobi = 0.0
        if sum_hiAgreSumfee!=0:
            shiji_baoxiaobi = round( sum_hifpPay/sum_hiAgreSumfee* decimal.Decimal('10') ,1)
        obj2['shiji_baoxiaobi'] = shiji_baoxiaobi

        # 计算 renci_rentoubi 
        renci_rentoubi = 11.0
        if cnt_psnNo !=0:
            renci_rentoubi = round( 11 - cnt_renci/cnt_psnNo , 1)
        obj2['renci_rentoubi'] = renci_rentoubi

        lst1.append(obj2)


    _saveStatist33(lst1)


# 该方法刷新数据表 t_statist_34， 
# 对应 《运行分析表.xlsx》的 第4个表
# 评价指标 - 2024年？月份居民门诊综合评价（多维条形图) 例如 医院jibie=1  year=2024  month=7
def refreshStatist34( year, month):  

    dangyueStartTime = datetime(year,month,1)
    if month == 12:
        dangyueEndTime = datetime(year+1,1,1)
    else:
        dangyueEndTime = datetime(year,month+1,1)

    # 获取 一级医院清单
    sql9="select * from t_qinghe_ddyy where jibie=1 order by sort_no"
    param9 = {}
    resDdyy = SqlHelper.fetch_all(sql9, param9)

    lst1 = []
    # 遍历 一级定点 医院，组装 lst1 
    for objyy in resDdyy:
        obj2 = {
            'year_val': year,
            'month_val': month,
            'fixmedins_code': objyy['fixmedins_code'] ,
            'fixmedins_name': objyy['fixmedins_name'] ,

            'renjun_menzhen_feiyong': 0.0,
            'shiji_baoxiaobi': 0.0,
            'rijun_renci': 0.0,
            'renci_rentoubi': 0.0,
            'jiuzhen_nianling': 0.0,
            'sort_no': objyy['sort_no']
        }

        sum_hiAgreSumfee = decimal.Decimal('0')
        sum_hifpPay =decimal.Decimal('0')
        _, monthDays = calendar.monthrange( year, month)  
        cnt_psnNo = decimal.Decimal('0')
        cnt_renci = decimal.Decimal('0')
        cnt_age = decimal.Decimal('0')

        sqlFrom = " from t_ybtool1_jiesuan_mingxi where setlTime between %(startSetlTime)s and %(endSetlTime)s\
             and fixmedinsCode = %(fixmedinsCode)s and insutype =%(insutype)s and medType = %(medType)s"
        paramFrom = {'startSetlTime':dangyueStartTime,'endSetlTime': dangyueEndTime,
                     'fixmedinsCode': obj2['fixmedins_code'],'insutype': '390' ,'medType':'11',}
        # 获取 sum_hiAgreSumfee   sum_hifpPay 
        sql8 = f" select fixmedinsCode, sum(hiAgreSumfee) as sum_hiAgreSumfee,  \
            sum(hifpPay) as sum_hifpPay \
                  {sqlFrom} group by fixmedinsCode"
        res8 = SqlHelper.fetch_one(sql=sql8,args=paramFrom)
        
        if res8 is not None:
            sum_hiAgreSumfee = res8['sum_hiAgreSumfee']
            sum_hifpPay = res8['sum_hifpPay']

        # 获取 cnt_psnNo 
        sql7 = f"select count( distinct psnNo) as cnt_psnNo {sqlFrom} "
        res7 = SqlHelper.fetch_one(sql=sql7,args=paramFrom)
        if res7 is not None:
            cnt_psnNo = res7['cnt_psnNo']

        # 获取 cnt_renci
        sql6 = "select count(1) as cnt_renci from t_ybtool1_jiesuan_mingxi where setlTime between %(startSetlTime)s and %(endSetlTime)s\
             and fixmedinsCode = %(fixmedinsCode)s group by fixmedinsCode"
        res6 = SqlHelper.fetch_one(sql=sql6,args=paramFrom)
        if res6 is not None:
            cnt_renci = res6['cnt_renci']

        # 获取 cnt_age
        sql5 = "select count(1) as cnt_age from t_ybtool1_jiesuan_mingxi \
            where setlTime between %(startSetlTime)s and %(endSetlTime)s\
            and fixmedinsCode = %(fixmedinsCode)s and age between 15 and 45"
        res5 = SqlHelper.fetch_one(sql=sql5,args=paramFrom)
        if res5 is not None:
            cnt_age = res5['cnt_age']

        # 计算 renjun_menzhen_feiyong  
        renjun_menzhen_feiyong = 10.0
        if cnt_renci != 0:
            renjun_menzhen_feiyong = round(10 - sum_hiAgreSumfee/cnt_renci/decimal.Decimal('10'), 1)
        obj2['renjun_menzhen_feiyong'] = renjun_menzhen_feiyong

        # 计算 shiji_baoxiaobi
        shiji_baoxiaobi = 0.0
        if sum_hiAgreSumfee!=0:
            shiji_baoxiaobi = round( sum_hifpPay/sum_hiAgreSumfee* decimal.Decimal('10') ,1)
        obj2['shiji_baoxiaobi'] = shiji_baoxiaobi

        # 计算 rijun_renci
        rijun_renci = 10.0
        if monthDays!=0:
            rijun_renci = round(10- cnt_renci/ decimal.Decimal(monthDays)/ decimal.Decimal('100'),1)
        obj2['rijun_renci'] = rijun_renci

        # 计算 renci_rentoubi 
        renci_rentoubi = 11.0
        if cnt_psnNo !=0:
            renci_rentoubi = round( 11 - cnt_renci/cnt_psnNo , 1)
        obj2['renci_rentoubi'] = renci_rentoubi

        #计算 jiuzhen_nianling
        jiuzhen_nianling = 10.0 
        if cnt_renci !=0:
            jiuzhen_nianling = 10-round( cnt_age/cnt_renci , 1)
        obj2['jiuzhen_nianling'] = jiuzhen_nianling


        lst1.append(obj2)


    _saveStatist34(lst1)


# 该方法刷新数据表 t_statist_35， 
# 对应 《运行分析表.xlsx》的 第5个表
# 评价指标 - 2024年？月份就诊病种（词云)   year=2024  month=7
def refreshStatist35( year, month):  

    dangyueStartTime = datetime(year,month,1)
    if month == 12:
        dangyueEndTime = datetime(year+1,1,1)
    else:
        dangyueEndTime = datetime(year,month+1,1)

    # 获取当月 的top 25 的 diseName
    sql9 = "select diseNo, diseName, count(1) as cnt1  from t_ybtool1_jiesuan_mingxi \
                where setlTime between %(startSetlTime)s and %(endSetlTime)s  \
                and diseName is not null and diseName !=''  \
                group by diseNo, diseName order by cnt1 desc limit 25 "
    param9 = {'startSetlTime':dangyueStartTime,'endSetlTime': dangyueEndTime }
    res9 = SqlHelper.fetch_all(sql=sql9, args=param9)

    # 清除历史数据
    sql8 = "delete from t_statist_35 where year_val=%(year_val)s and month_val=%(month_val)s"      
    param9 = {"year_val":year, "month_val": month }
    SqlHelper.delete(sql=sql8, args=param9)

    # 保存数据到 t_statist_35
    if res9 is not None:
        for objct in res9:
            obj2 = {
                'year_val': year, 'month_val': month,
                'dise_name': objct['diseName'], 'dist_count': objct['cnt1'], 'sort_no' :  objct['cnt1']
            }

            sql9 = "insert into t_statist_35 (id,year_val,month_val,dise_name,dist_count,sort_no) \
                    values ( %(id)s,%(year_val)s,%(month_val)s,%(dise_name)s,%(dist_count)s, %(sort_no)s ) "
            param9={"id": uuid.uuid4().hex, "year_val": obj2['year_val'], "month_val": obj2['month_val'], 
                "dise_name":obj2['dise_name'], "dist_count":obj2['dist_count'],  "sort_no": obj2['sort_no']
                    }
            SqlHelper.insert(sql9, param9)


# 该方法刷新数据表 t_statist_41， 
# 对应 《运行分析表.xlsx》的
# 考核指标 - 2024年？月份城乡居民基本医疗保险门诊统筹就诊情况  year=2024  month=7
def refreshStatist41(year, month): 
    srcTableName = 't_ybtool1_jiesuan_mingxi'
    if year == 2023:
        srcTableName = 't_ybtool1_jiesuan_mingxi_2023'

    dangyueStartTime = datetime(year,month,1)
    if month == 12:
        dangyueEndTime = datetime(year+1,1,1)
    else:
        dangyueEndTime = datetime(year,month+1,1)

    # 获取 一级医院清单
    sql9="select * from t_qinghe_ddyy where jibie=1 order by sort_no"
    param9 = {}
    resDdyy = SqlHelper.fetch_all(sql9, param9)

    lst1 = []
    # 遍历 一级定点 医院，组装 lst1 
    for objyy in resDdyy:
        obj2 = {
            'year_val': year,
            'month_val': month,
            'fixmedins_code': objyy['fixmedins_code'] ,
            'fixmedins_name': objyy['fixmedins_name'] ,

            'jiuzhen_renci': 0.0,
            'jiuzhen_renshu': 0.0,
            'renci_rentoubi': 0.0,
            'yiliao_zongfeiyong': 0.0,
            'tongchou_jijin_zhifu': 0.0,
            'cijun_yiliao_feiyong': 0.0,
            'renjun_yiliao_feiyong': 0.0,
            'sort_no': objyy['sort_no']
        }

        sum_hiAgreSumfee = decimal.Decimal('0')
        sum_hifpPay =decimal.Decimal('0')
        cnt_psnNo = decimal.Decimal('0')
        cnt_renci = decimal.Decimal('0')

        sqlFrom = f" from {srcTableName} where setlTime between %(startSetlTime)s and %(endSetlTime)s \
             and insuAdmdvs='130534' and fixmedinsCode = %(fixmedinsCode)s and insutype =%(insutype)s and medType = %(medType)s"
        paramFrom = {'startSetlTime':dangyueStartTime,'endSetlTime': dangyueEndTime,
                     'fixmedinsCode': obj2['fixmedins_code'],'insutype': '390' ,'medType':'11',}
        # 获取 sum_hiAgreSumfee   sum_hifpPay 
        sql8 = f" select fixmedinsCode, sum(hiAgreSumfee) as sum_hiAgreSumfee,  \
            sum(hifpPay) as sum_hifpPay \
                  {sqlFrom} group by fixmedinsCode"
        res8 = SqlHelper.fetch_one(sql=sql8,args=paramFrom)
        
        if res8 is not None:
            sum_hiAgreSumfee = res8['sum_hiAgreSumfee']
            sum_hifpPay = res8['sum_hifpPay']

        # 获取 cnt_psnNo 
        sql7 = f"select count( distinct psnNo) as cnt_psnNo {sqlFrom} "
        res7 = SqlHelper.fetch_one(sql=sql7,args=paramFrom)
        if res7 is not None:
            cnt_psnNo = res7['cnt_psnNo']

        # 获取 cnt_renci
        sql6 = f"select count(1) as cnt_renci from {srcTableName} \
            where setlTime between %(startSetlTime)s and %(endSetlTime)s\
             and fixmedinsCode = %(fixmedinsCode)s group by fixmedinsCode"
        res6 = SqlHelper.fetch_one(sql=sql6,args=paramFrom)
        if res6 is not None:
            cnt_renci = res6['cnt_renci']

        # 计算 jiuzhen_renci  
        jiuzhen_renci = cnt_renci
        obj2['jiuzhen_renci'] = jiuzhen_renci

        # 计算 jiuzhen_renshu
        jiuzhen_renshu = cnt_psnNo
        obj2['jiuzhen_renshu'] = jiuzhen_renshu

        # 计算 renci_rentoubi
        renci_rentoubi = 0.0
        if cnt_psnNo!=0:
            renci_rentoubi = round(  cnt_renci/ cnt_psnNo,1)
        obj2['renci_rentoubi'] = renci_rentoubi

        # 计算 yiliao_zongfeiyong 
        yiliao_zongfeiyong = sum_hiAgreSumfee
        obj2['yiliao_zongfeiyong'] = yiliao_zongfeiyong

        #计算 tongchou_jijin_zhifu
        tongchou_jijin_zhifu = sum_hifpPay
        obj2['tongchou_jijin_zhifu'] = tongchou_jijin_zhifu

        # 计算 cijun_yiliao_feiyong
        cijun_yiliao_feiyong = 0.0
        if cnt_renci!=0:
            cijun_yiliao_feiyong = round(  sum_hiAgreSumfee/ cnt_renci,1)
        obj2['cijun_yiliao_feiyong'] = cijun_yiliao_feiyong

        # 计算 renjun_yiliao_feiyong
        renjun_yiliao_feiyong = 0.0
        if cnt_psnNo!=0:
            renjun_yiliao_feiyong = round(  sum_hiAgreSumfee/ cnt_psnNo,1)
        obj2['renjun_yiliao_feiyong'] = renjun_yiliao_feiyong

        lst1.append(obj2)

    _saveStatist41(lst1)


# 该方法刷新数据表 t_statist_42， 
# 对应 《运行分析表.xlsx》的
# 考核指标 - 2024年？月份城乡居民基本医疗保险住院评价指标  year=2024  month=7
def refreshStatist42(year, month): 

    srcTableName = 't_ybtool1_jiesuan_mingxi'
    if year == 2023:
        srcTableName = 't_ybtool1_jiesuan_mingxi_2023'

    dangyueStartTime = datetime(year,month,1)
    if month == 12:
        dangyueEndTime = datetime(year+1,1,1)
    else:
        dangyueEndTime = datetime(year,month+1,1)

    # 获取 所有医院清单
    sql9="select * from t_qinghe_ddyy   order by sort_no"
    param9 = {}
    resDdyy = SqlHelper.fetch_all(sql9, param9)

    lst1 = []
    # 遍历 定点医院，组装 lst1 
    for objyy in resDdyy:
        obj2 = {
            'year_val': year,
            'month_val': month,
            'fixmedins_code': objyy['fixmedins_code'] ,
            'fixmedins_name': objyy['fixmedins_name'] ,
            
            'yiliao_zongfeiyong': 0.0,
            'jiuzhen_renci': 0.0,
            'jiuzhen_renshu': 0.0,
            'cijun_feiyong': 0.0,
            'renjun_feiyong': 0.0,
            'tongchou_jijin_zhifu': 0.0,
            'shiji_baoxiao_bili': 0.0,
            'zhuyuan_tianshu': 0.0,
            'cijun_zhuyuan_tianshu': 0.0,
            'renjun_zhuyuan_tianshu': 0.0,
            'zhuyuan_pingjun_chuangri_feiyong': 0.0,
            'sort_no': objyy['sort_no']
        }

        sum_hiAgreSumfee = decimal.Decimal('0')
        sum_hifpPay =decimal.Decimal('0')
        cnt_psnNo = decimal.Decimal('0')
        cnt_renci = decimal.Decimal('0')
        sumDays = decimal.Decimal('0')

        sqlFrom = f" from {srcTableName} where setlTime between %(startSetlTime)s and %(endSetlTime)s \
             and insuAdmdvs='130534' and fixmedinsCode = %(fixmedinsCode)s and insutype =%(insutype)s and medType = %(medType)s"
        paramFrom = {'startSetlTime':dangyueStartTime,'endSetlTime': dangyueEndTime,
                     'fixmedinsCode': obj2['fixmedins_code'],'insutype': '390' ,'medType':'11',}
        # 获取 sum_hiAgreSumfee   sum_hifpPay 
        sql8 = f" select fixmedinsCode, sum(hiAgreSumfee) as sum_hiAgreSumfee,  \
            sum(hifpPay) as sum_hifpPay \
                  {sqlFrom} group by fixmedinsCode"
        res8 = SqlHelper.fetch_one(sql=sql8,args=paramFrom)
        
        if res8 is not None:
            sum_hiAgreSumfee = res8['sum_hiAgreSumfee']
            sum_hifpPay = res8['sum_hifpPay']

        # 获取 cnt_psnNo 
        sql7 = f"select count( distinct psnNo) as cnt_psnNo {sqlFrom} "
        res7 = SqlHelper.fetch_one(sql=sql7,args=paramFrom)
        if res7 is not None:
            cnt_psnNo = res7['cnt_psnNo']

        # 获取 cnt_renci 
        sql6 = f"select count(1) as cnt_renci, sum( datediff(enddate,begndate) +1 ) as sumDays from {srcTableName} \
            where setlTime between %(startSetlTime)s and %(endSetlTime)s\
             and fixmedinsCode = %(fixmedinsCode)s group by fixmedinsCode"
        res6 = SqlHelper.fetch_one(sql=sql6,args=paramFrom)
        if res6 is not None:
            cnt_renci = res6['cnt_renci']
            sumDays = res6['sumDays']

        # 计算 yiliao_zongfeiyong 
        yiliao_zongfeiyong = sum_hiAgreSumfee
        obj2['yiliao_zongfeiyong'] = yiliao_zongfeiyong

        # 计算 jiuzhen_renci  
        jiuzhen_renci = cnt_renci
        obj2['jiuzhen_renci'] = jiuzhen_renci

        # 计算 jiuzhen_renshu
        jiuzhen_renshu = cnt_psnNo
        obj2['jiuzhen_renshu'] = jiuzhen_renshu

        #计算 tongchou_jijin_zhifu
        tongchou_jijin_zhifu = sum_hifpPay
        obj2['tongchou_jijin_zhifu'] = tongchou_jijin_zhifu

        # 计算 cijun_feiyong
        cijun_feiyong = 0.0
        if cnt_renci!=0:
            cijun_feiyong = round(  sum_hiAgreSumfee/ cnt_renci,1)
        obj2['cijun_feiyong'] = cijun_feiyong

        # 计算 renjun_feiyong
        renjun_feiyong = 0.0
        if cnt_psnNo!=0:
            renjun_feiyong = round(  sum_hiAgreSumfee/ cnt_psnNo ,1)
        obj2['renjun_feiyong'] = renjun_feiyong

        # 计算 shiji_baoxiao_bili
        shiji_baoxiao_bili = 0.0
        if sum_hiAgreSumfee!=0:
            shiji_baoxiao_bili = round(  tongchou_jijin_zhifu/ sum_hiAgreSumfee,1)
        obj2['shiji_baoxiao_bili'] = shiji_baoxiao_bili

        # 计算 cijun_zhuyuan_tianshu
        cijun_zhuyuan_tianshu = 0.0
        if cnt_renci!=0:
            cijun_zhuyuan_tianshu = round(  sumDays/ cnt_renci,1)
        obj2['cijun_zhuyuan_tianshu'] = cijun_zhuyuan_tianshu

        # 计算 renjun_zhuyuan_tianshu
        renjun_zhuyuan_tianshu = 0.0
        if cnt_psnNo !=0:
            renjun_zhuyuan_tianshu = round(  sumDays/ cnt_psnNo ,1)
        obj2['renjun_zhuyuan_tianshu'] = renjun_zhuyuan_tianshu

        # 计算 zhuyuan_pingjun_chuangri_feiyong
        zhuyuan_pingjun_chuangri_feiyong = 0.0
        if sumDays !=0:
            zhuyuan_pingjun_chuangri_feiyong = round(  sum_hiAgreSumfee/ sumDays ,1)
        obj2['zhuyuan_pingjun_chuangri_feiyong'] = zhuyuan_pingjun_chuangri_feiyong
        

        lst1.append(obj2)

    _saveStatist42(lst1)



# 该方法刷新数据表 t_statist_51， 
# 对应 《运行分析表.xlsx》的
# 医院考核  year=2024
def refreshStatist51(year): 



    # 获取 所有医院清单
    sql9="select * from t_qinghe_ddyy   order by sort_no"
    param9 = {}
    resDdyy = SqlHelper.fetch_all(sql9, param9)
    
    if resDdyy is None:
        print('???????  由于未知原因，未能获取定点医疗机构清单')
        return
    
    # 遍历每个医院，计算每个医院
    for ddyy in resDdyy:
        fixmedinsCode = ddyy['fixmedins_code']

        print(f" ===== 开始统计： {fixmedinsCode}-{ddyy['fixmedins_name']}  的统筹基金支付金额 ")


        for month1 in  range(1,13):
            startTime = datetime(year,month1,1)
            if month1==12:
                endTime = datetime(year+1,1,1)
            else:
                endTime = datetime(year,month1+1,1)

            sql9 = "select sum(hifpPay) as sum_hifpPay from t_ybtool1_jiesuan_mingxi where \
                fixmedinsCode=%(fixmedinsCode)s and setlTime between %(startSetlTime)s and %(endSetlTime)s \
                and insuType=%(insuType)s and medType=%(medType)s and insuAdmdvs='130534' group by fixmedinsCode "
            

            # 计算 该医院 insuType=390 medType=21 
            # 计算 该医院 insuType=310 medType=21 
            # 计算 该医院 insuType=390 medType=11 
            # 计算 该医院 insuType=310 medType=11
            # 计算 该医院 insuType=390 medType=14 
            # 计算 该医院 insuType=310 medType=14
            for insuType1 in ['390','310']:
                for medType1 in ['21','11','14']:

                    
                    param9 = {'fixmedinsCode': fixmedinsCode, 
                              "startSetlTime": startTime,"endSetlTime":endTime,
                              'insuType':insuType1, 'medType':medType1}
                    res9 = SqlHelper.fetch_one(sql9, param9)

                    sum_hifpPay = decimal.Decimal('0')
                    if res9 is not None:
                        sum_hifpPay = res9['sum_hifpPay']

                    obj8 ={
                        'year_val':year,
                        'month_val': month1,
                        'fixmedins_code': fixmedinsCode ,
                        'fixmedins_name': ddyy['fixmedins_name'],
                        'insu_type': insuType1,
                        'med_type': medType1,
                        'tongchou_jijin_zhifu': sum_hifpPay,
                        'sort_no': month1
                    }

                    # print(f" ===== 统计结果： {fixmedinsCode}-{ddyy['fixmedins_name']} {year}年{month1}月 insuType={insuType1} medType={medType1} 的统筹基金支付金额 ： {sum_hifpPay}")

                    _saveStatist51(obj8)

                    
# 该方法刷新数据表 t_month_zongyusuan  
def refreshMonthZongyusuan(year): 
    sql9 = "select sum(yusuan_money) as zongyusuan from t_year_yusuan \
          where year_val = %(year_val)s and yusuan_title in \
            ('县内住院','门诊统筹','门诊慢性病','生育','市内住院','市外住院','采集')  group by year_val"
    param9 = {"year_val":year }
    res9 = SqlHelper.fetch_one(sql9, param9)
    if res9 is None:
        print(f"未能获取年度预算")
        return
        
    zongyusuan =  res9['zongyusuan']
    monthYusuan = round( zongyusuan / decimal.Decimal('12') ,3)

    # 将月度总预算保存到 t_month_zongyusuan 表
    for i1 in range(12):
        month1 = i1+1 

        sql8 = 'delete from t_month_zongyusuan where year_val = %(year_val)s \
              and month_val=%(month_val)s   '
        sql9 = "insert into t_month_zongyusuan (id,year_val,month_val,yusuan_money ) \
                    values ( %(id)s,%(year_val)s,%(month_val)s,%(yusuan_money)s ) "
        param9 = {'id': uuid.uuid4().hex ,'year_val': year,'month_val': month1,'yusuan_money': monthYusuan}
        
        SqlHelper.delete(sql8, param9)
        SqlHelper.insert(sql9, param9)
        
        

    


def _saveStatist31(lst):
    
    for obj in lst:
        sql9 = "insert into t_statist_31 (id,year_val,month_val,fixmedins_code,fixmedins_name,\
            insu_type,cijun_feiyong,pingjun_zhuyuan_tianshu,rijun_feiyong,shiji_baoxiaobi,\
                renci_rentoubi,yidi_zhanbi,sort_no) \
                    values ( %(id)s,%(year_val)s,%(month_val)s,%(fixmedins_code)s,%(fixmedins_name)s,\
            %(insu_type)s,%(cijun_feiyong)s,%(pingjun_zhuyuan_tianshu)s,%(rijun_feiyong)s,%(shiji_baoxiaobi)s,\
            %(renci_rentoubi)s,%(yidi_zhanbi)s, %(sort_no)s ) \
            ON DUPLICATE KEY UPDATE \
            fixmedins_name=VALUES(fixmedins_name),\
            cijun_feiyong=VALUES(cijun_feiyong),\
            pingjun_zhuyuan_tianshu=VALUES(pingjun_zhuyuan_tianshu),\
            rijun_feiyong=VALUES(rijun_feiyong),\
            shiji_baoxiaobi=VALUES(shiji_baoxiaobi),\
            renci_rentoubi=VALUES(renci_rentoubi),\
            yidi_zhanbi=VALUES(yidi_zhanbi),\
            sort_no=VALUES(sort_no) \
                "
        param9={"id": uuid.uuid4().hex, "year_val": obj['year_val'], "month_val": obj['month_val'], 
            "fixmedins_code":obj['fixmedins_code'], "fixmedins_name":obj['fixmedins_name'],
            "insu_type":obj['insu_type'], "cijun_feiyong":obj['cijun_feiyong'],
            "pingjun_zhuyuan_tianshu":obj['pingjun_zhuyuan_tianshu'], "rijun_feiyong":obj['rijun_feiyong'],
            "shiji_baoxiaobi":obj['shiji_baoxiaobi'], "renci_rentoubi":obj['renci_rentoubi'],
            "yidi_zhanbi":obj['yidi_zhanbi'], 
            "sort_no": obj['sort_no']
                }
        
        SqlHelper.insert(sql9, param9)
    

def _saveStatist33(lst):
    
    for obj in lst:
        sql9 = "insert into t_statist_33 (id,year_val,month_val,fixmedins_code,fixmedins_name,\
            cijun_feiyong,pingjun_zhuyuan_tianshu,rijun_feiyong,shiji_baoxiaobi,\
                renci_rentoubi,sort_no) \
                    values ( %(id)s,%(year_val)s,%(month_val)s,%(fixmedins_code)s,%(fixmedins_name)s,\
            %(cijun_feiyong)s,%(pingjun_zhuyuan_tianshu)s,%(rijun_feiyong)s,%(shiji_baoxiaobi)s,\
            %(renci_rentoubi)s, %(sort_no)s ) \
            ON DUPLICATE KEY UPDATE \
            fixmedins_name=VALUES(fixmedins_name),\
            cijun_feiyong=VALUES(cijun_feiyong),\
            pingjun_zhuyuan_tianshu=VALUES(pingjun_zhuyuan_tianshu),\
            rijun_feiyong=VALUES(rijun_feiyong),\
            shiji_baoxiaobi=VALUES(shiji_baoxiaobi),\
            renci_rentoubi=VALUES(renci_rentoubi),\
            sort_no=VALUES(sort_no) \
                "
        param9={"id": uuid.uuid4().hex, "year_val": obj['year_val'], "month_val": obj['month_val'], 
            "fixmedins_code":obj['fixmedins_code'], "fixmedins_name":obj['fixmedins_name'],
            "cijun_feiyong":obj['cijun_feiyong'],
            "pingjun_zhuyuan_tianshu":obj['pingjun_zhuyuan_tianshu'], "rijun_feiyong":obj['rijun_feiyong'],
            "shiji_baoxiaobi":obj['shiji_baoxiaobi'], "renci_rentoubi":obj['renci_rentoubi'],
            "sort_no": obj['sort_no']
                }
        
        SqlHelper.insert(sql9, param9)
    

def _saveStatist34(lst):
    
    for obj in lst:
        sql9 = "insert into t_statist_34 (id,year_val,month_val,fixmedins_code,fixmedins_name,\
            renjun_menzhen_feiyong,shiji_baoxiaobi,rijun_renci,renci_rentoubi,\
                jiuzhen_nianling,sort_no) \
                    values ( %(id)s,%(year_val)s,%(month_val)s,%(fixmedins_code)s,%(fixmedins_name)s,\
            %(renjun_menzhen_feiyong)s,%(shiji_baoxiaobi)s,%(rijun_renci)s,%(renci_rentoubi)s,\
            %(jiuzhen_nianling)s, %(sort_no)s ) \
            ON DUPLICATE KEY UPDATE \
            fixmedins_name=VALUES(fixmedins_name),\
            renjun_menzhen_feiyong=VALUES(renjun_menzhen_feiyong),\
            shiji_baoxiaobi=VALUES(shiji_baoxiaobi),\
            rijun_renci=VALUES(rijun_renci),\
            renci_rentoubi=VALUES(renci_rentoubi),\
            jiuzhen_nianling=VALUES(jiuzhen_nianling),\
            sort_no=VALUES(sort_no) \
                "
        param9={"id": uuid.uuid4().hex, "year_val": obj['year_val'], "month_val": obj['month_val'], 
            "fixmedins_code":obj['fixmedins_code'], "fixmedins_name":obj['fixmedins_name'],
            "renjun_menzhen_feiyong":obj['renjun_menzhen_feiyong'],
            "shiji_baoxiaobi":obj['shiji_baoxiaobi'],
            "rijun_renci":obj['rijun_renci'],
            "renci_rentoubi":obj['renci_rentoubi'], 
            "jiuzhen_nianling":obj['jiuzhen_nianling'],
            "sort_no": obj['sort_no']
                }
        
        SqlHelper.insert(sql9, param9)



def _saveStatist41(lst):

    for obj in lst:
        sql8 = 'delete from t_statist_41 where year_val = %(year_val)s and month_val=%(month_val)s and fixmedins_code=%(fixmedins_code)s '

        sql9 = "insert into t_statist_41 (id,year_val,month_val,fixmedins_code,fixmedins_name,\
            jiuzhen_renci,jiuzhen_renshu,renci_rentoubi,yiliao_zongfeiyong,tongchou_jijin_zhifu,\
                cijun_yiliao_feiyong,renjun_yiliao_feiyong,sort_no) \
                    values ( %(id)s,%(year_val)s,%(month_val)s,%(fixmedins_code)s,%(fixmedins_name)s,\
            %(jiuzhen_renci)s,%(jiuzhen_renshu)s,%(renci_rentoubi)s,%(yiliao_zongfeiyong)s,%(tongchou_jijin_zhifu)s,\
            %(cijun_yiliao_feiyong)s,%(renjun_yiliao_feiyong)s, %(sort_no)s ) "
        param9={"id": uuid.uuid4().hex, "year_val": obj['year_val'], "month_val": obj['month_val'], 
            "fixmedins_code":obj['fixmedins_code'], "fixmedins_name":obj['fixmedins_name'],
            "jiuzhen_renci":obj['jiuzhen_renci'],"jiuzhen_renshu":obj['jiuzhen_renshu'],
            "renci_rentoubi":obj['renci_rentoubi'], "yiliao_zongfeiyong":obj['yiliao_zongfeiyong'],
            "tongchou_jijin_zhifu":obj['tongchou_jijin_zhifu'], "cijun_yiliao_feiyong":obj['cijun_yiliao_feiyong'],
            "renjun_yiliao_feiyong":obj['renjun_yiliao_feiyong'],
            "sort_no": obj['sort_no']
                }
        SqlHelper.delete(sql8, param9)
        SqlHelper.insert(sql9, param9)
    

def _saveStatist42(lst):

    for obj in lst:
        sql8 = 'delete from t_statist_42 where year_val = %(year_val)s and month_val=%(month_val)s and fixmedins_code=%(fixmedins_code)s '

        sql9 = "insert into t_statist_42 (id,year_val,month_val,fixmedins_code,fixmedins_name,\
            yiliao_zongfeiyong,jiuzhen_renci,jiuzhen_renshu,\
            cijun_feiyong,renjun_feiyong,tongchou_jijin_zhifu,shiji_baoxiao_bili,\
            zhuyuan_tianshu,cijun_zhuyuan_tianshu,\
            renjun_zhuyuan_tianshu, zhuyuan_pingjun_chuangri_feiyong, sort_no) \
                    values ( %(id)s,%(year_val)s,%(month_val)s,%(fixmedins_code)s,%(fixmedins_name)s,\
            %(yiliao_zongfeiyong)s,%(jiuzhen_renci)s,%(jiuzhen_renshu)s,\
            %(cijun_feiyong)s,%(renjun_feiyong)s,%(tongchou_jijin_zhifu)s,%(shiji_baoxiao_bili)s,\
            %(zhuyuan_tianshu)s,%(cijun_zhuyuan_tianshu)s,\
            %(renjun_zhuyuan_tianshu)s,%(zhuyuan_pingjun_chuangri_feiyong)s, %(sort_no)s ) "
        
        param9={"id": uuid.uuid4().hex, "year_val": obj['year_val'], "month_val": obj['month_val'], 
            "fixmedins_code":obj['fixmedins_code'], "fixmedins_name":obj['fixmedins_name'],
            "yiliao_zongfeiyong":obj['yiliao_zongfeiyong'],"jiuzhen_renci":obj['jiuzhen_renci'],"jiuzhen_renshu":obj['jiuzhen_renshu'],
            "cijun_feiyong":obj['cijun_feiyong'], "renjun_feiyong":obj['renjun_feiyong'],
            "tongchou_jijin_zhifu":obj['tongchou_jijin_zhifu'], "shiji_baoxiao_bili":obj['shiji_baoxiao_bili'],
            "zhuyuan_tianshu":obj['zhuyuan_tianshu'],"cijun_zhuyuan_tianshu":obj['cijun_zhuyuan_tianshu'],
            "renjun_zhuyuan_tianshu":obj['renjun_zhuyuan_tianshu'],
            "zhuyuan_pingjun_chuangri_feiyong":obj['zhuyuan_pingjun_chuangri_feiyong'],
            "sort_no": obj['sort_no']
            }
        SqlHelper.delete(sql8, param9)
        SqlHelper.insert(sql9, param9)

def _saveStatist51(obj):
    sql8 = 'delete from t_statist_51 where year_val = %(year_val)s and month_val=%(month_val)s \
          and fixmedins_code=%(fixmedins_code)s and insu_type=%(insu_type)s and med_type=%(med_type)s'

    sql9 = "insert into t_statist_51 (id,year_val,month_val,fixmedins_code,fixmedins_name,\
        insu_type,med_type,tongchou_jijin_zhifu, sort_no) \
                values ( %(id)s,%(year_val)s,%(month_val)s,%(fixmedins_code)s,%(fixmedins_name)s,\
        %(insu_type)s,%(med_type)s,%(tongchou_jijin_zhifu)s, %(sort_no)s ) "
    
    param9={"id": uuid.uuid4().hex, "year_val": obj['year_val'], "month_val": obj['month_val'], 
        "fixmedins_code":obj['fixmedins_code'], "fixmedins_name":obj['fixmedins_name'],
        "insu_type":obj['insu_type'],"med_type":obj['med_type'],
        "tongchou_jijin_zhifu":obj['tongchou_jijin_zhifu'],
        "sort_no": obj['sort_no']
        }
    SqlHelper.delete(sql8, param9)
    SqlHelper.insert(sql9, param9)


#################  各种私有方法

# 获取年预算
def _getYearYusuan(year, yusuanTitle):
    sql9="select * from t_year_yusuan where year_val= %(yearVal)s and yusuan_title = %(yusuanTitle)s"
    param9 = {"yearVal": year,"yusuanTitle":yusuanTitle}
    res9 = SqlHelper.fetch_one(sql9, param9)
    
    if res9 is None:
        return 0
    else:
        return res9['yusuan_money']


# 获取清河定点医院的年预算
def _listQingheDdyyYearYusuan(zhichuType, year ):
    sql9="select * from t_qinghe_ddyy_year_yusuan where year_val= %(yearVal)s and yusuan_type = %(yusuan_type)s order by sort_no"
    param9 = {"yearVal": year,"yusuan_type":zhichuType}
    res9 = SqlHelper.fetch_all(sql9, param9)
    
    if res9 is None:
        return []
    else:
        return res9

# 列出清河所有定点医院
def _listQingheDdyy():
    sql9="select * from t_qinghe_ddyy order by sort_no"
    param9 = {}
    res9 = SqlHelper.fetch_all(sql9, param9)
    
    if res9 is None:
        return []
    else:
        return res9
    
# 获取根据编号获取定点医院
def _getQingheDdyyByCode( lst ,fixmedinsCode):
    for obj in lst:
        if obj['fixmedins_code'] == fixmedinsCode:
            return obj 
    return None

def _getMonthZongYusuan(year,month):
    sql9="select * from t_month_zongyusuan where year_val= %(yearVal)s and month_val = %(monthVal)s"
    param9 = {"yearVal": year,"monthVal":month}
    res9 = SqlHelper.fetch_one(sql9, param9)
    
    if res9 is None:
        return 0
    else:
        return res9['yusuan_money']
    

### 获取当月开始和结束时间
def _getMonthStartAndEndTime(year,month):
    # 获取月份的第一天  
    start_date = datetime(year, month, 1)  
      
    # 下一月的第一天的 0 点
    nextYear = year
    nextMonth = month+1
    if nextMonth >12:
        nextYear = year+1
        nextMonth = 1

    end_date = datetime(nextYear, nextMonth, 1)  
      
    # 返回开始和结束日期  
    return start_date, end_date  

#### 统计 支出金额
def _statistZhichu(startTime, endTime, subwhere):
    sql9 = f"select sum(hifpPay) as zhichu from t_ybtool1_jiesuan_mingxi where \
        setlTime between %(startSetlTime)s and %(endSetlTime)s {subwhere}"
    param9 = {"startSetlTime": startTime,"endSetlTime":endTime}
    res9 = SqlHelper.fetch_one(sql9, param9)
    
    return res9['zhichu'] if res9['zhichu'] is not None else decimal.Decimal("0.0")


### 计算进度
def _calcJindu(fenzi,fenmu):
    if fenzi is None:
        fenzi = 0.0
    
    if fenmu is None or fenmu ==0 :
        return {"val1":0, "val2":0, "val3": "0%" }
    
    val1 = fenzi/fenmu 
    val2 = round(val1,4) 
    val3 = "%.2f%%" % (val1 * 100) 
    return {"val1":val1, "val2":val2, "val3": val3 }

def _saveStatist01(year,month,zhichuLeixing, yusuan, 
                   dangyueZhichuMoney,wangyueZhichuMoney,yujiZhichuMoney,
                   dangyueZhichuJindu,wangyueZhichuJindu,yujiZhichuJindu,
                   sortNo):
    param9={"id": uuid.uuid4().hex, "yearVal": year, "monthVal": month, "zhichuLeixing": zhichuLeixing,
            "yusuanMoney":yusuan, "dangyueZhichuMoney":dangyueZhichuMoney,"wangyueZhichuMoney":wangyueZhichuMoney,
            "yujiZhichuMoney":yujiZhichuMoney,
            "dangyueZhichuJindu":dangyueZhichuJindu,"wangyueZhichuJindu":wangyueZhichuJindu,
            "yujiZhichuJindu":yujiZhichuJindu, "sortNo":sortNo }

    sql9 = "insert into t_statist_01 (id,year_val,month_val,zhichu_leixing,\
        yusuan_money,dangyue_zhichu_money,wangyue_zhichu_money,yuji_zhichu_money,\
            dangyue_zhichu_jindu,wangyue_zhichu_jindu,yuji_zhichu_jindu,sort_no) \
                values ( %(id)s,%(yearVal)s,%(monthVal)s,%(zhichuLeixing)s,\
                     %(yusuanMoney)s,%(dangyueZhichuMoney)s,%(wangyueZhichuMoney)s,%(yujiZhichuMoney)s,\
                    %(dangyueZhichuJindu)s,%(wangyueZhichuJindu)s,%(yujiZhichuJindu)s,%(sortNo)s ) \
        ON DUPLICATE KEY UPDATE \
          yusuan_money=VALUES(yusuan_money),\
          dangyue_zhichu_money=VALUES(dangyue_zhichu_money),\
          wangyue_zhichu_money=VALUES(wangyue_zhichu_money),\
          yuji_zhichu_money=VALUES(yuji_zhichu_money),\
          dangyue_zhichu_jindu=VALUES(dangyue_zhichu_jindu),\
          wangyue_zhichu_jindu=VALUES(wangyue_zhichu_jindu),\
          yuji_zhichu_jindu=VALUES(yuji_zhichu_jindu),\
          sort_no=VALUES(sort_no)\
            "
    SqlHelper.insert(sql9, param9)

def _saveStatist02(year, month, yusuanMoney, dangyueZhichuMoney, monthZhichuWanchenglv, monthLeijiZhichulv, sortNo):

    sqldel = "delete from t_statist_02 where year_val =%(yearVal)s and month_val = %(monthVal)s "
    sql9 = "insert into t_statist_02 (id,year_val,month_val,yusuan_money,\
        dangyue_zhichu_money,dangyue_zhichu_wanchenglv,dangyue_leiji_zhichulv,sort_no) \
            values ( %(id)s,%(yearVal)s,%(monthVal)s,%(yusuanMoney)s,\
            %(dangyueZhichuMoney)s,%(monthZhichuWanchenglv)s,%(monthLeijiZhichulv)s,%(sortNo)s ) \
            "
    param9={"id": uuid.uuid4().hex, "yearVal": year, "monthVal": month, 
           "yusuanMoney":yusuanMoney, "dangyueZhichuMoney":dangyueZhichuMoney,
           "monthZhichuWanchenglv":monthZhichuWanchenglv,"monthLeijiZhichulv":monthLeijiZhichulv,
           "sortNo": sortNo
            }
    
    SqlHelper.delete(sqldel, param9)
    SqlHelper.insert(sql9, param9)


def _saveStatist03(year,zhichuQuyu,qunianLeijiMoney,dangnianLeijiMoney,
                   qunianLeijiBili,dangnianLeijiBili,sortNo):
    

    sql9 = "insert into t_statist_03 (id,year_val,zhichu_quyu,\
        qunian_leiji_money,dangnian_leiji_money,\
            qunian_leiji_bili,dangnian_leiji_bili,sort_no) \
                values ( %(id)s,%(year_val)s,%(zhichu_quyu)s,\
        %(qunian_leiji_money)s,%(dangnian_leiji_money)s,\
        %(qunian_leiji_bili)s,%(dangnian_leiji_bili)s ,%(sort_no)s ) \
        ON DUPLICATE KEY UPDATE \
          qunian_leiji_money=VALUES(qunian_leiji_money),\
          dangnian_leiji_money=VALUES(dangnian_leiji_money),\
          qunian_leiji_bili=VALUES(qunian_leiji_bili),\
          dangnian_leiji_bili=VALUES(dangnian_leiji_bili),\
          sort_no=VALUES(sort_no) \
            "
    param9={"id": uuid.uuid4().hex, "year_val": year, "zhichu_quyu": zhichuQuyu, 
           "qunian_leiji_money":qunianLeijiMoney, "dangnian_leiji_money":dangnianLeijiMoney,
           "qunian_leiji_bili":qunianLeijiBili, "dangnian_leiji_bili":dangnianLeijiBili,
           "sort_no": sortNo
            }
    
    SqlHelper.insert(sql9, param9)



def _saveStatist21(lst):
    
    for obj in lst:
        sql9 = "insert into t_statist_21 (id,year_val,month_val,fixmedins_code,fixmedins_name,\
            zhichu_type,year_yusuan,curr_month_zhichu,leiji_zhichu,curr_month_zhichu_jindu,\
                leiji_zhichu_jindu,yuji_zhichu,yuji_zhichu_jindu,sort_no) \
                    values ( %(id)s,%(year_val)s,%(month_val)s,%(fixmedins_code)s,%(fixmedins_name)s,\
            %(zhichu_type)s,%(year_yusuan)s,%(curr_month_zhichu)s,%(leiji_zhichu)s,%(curr_month_zhichu_jindu)s,\
            %(leiji_zhichu_jindu)s,%(yuji_zhichu)s,%(yuji_zhichu_jindu)s ,%(sort_no)s ) \
            ON DUPLICATE KEY UPDATE \
            fixmedins_name=VALUES(fixmedins_name),\
            year_yusuan=VALUES(year_yusuan),\
            curr_month_zhichu=VALUES(curr_month_zhichu),\
            leiji_zhichu=VALUES(leiji_zhichu),\
            curr_month_zhichu_jindu=VALUES(curr_month_zhichu_jindu),\
            leiji_zhichu_jindu=VALUES(leiji_zhichu_jindu),\
            yuji_zhichu=VALUES(yuji_zhichu),\
            yuji_zhichu_jindu=VALUES(yuji_zhichu_jindu),\
            sort_no=VALUES(sort_no) \
                "
        param9={"id": uuid.uuid4().hex, "year_val": obj['year_val'], "month_val": obj['month_val'], 
            "fixmedins_code":obj['fixmedins_code'], "fixmedins_name":obj['fixmedins_name'],
            "zhichu_type":obj['zhichu_type'], "year_yusuan":obj['year_yusuan'],
            "curr_month_zhichu":obj['curr_month_zhichu'], "leiji_zhichu":obj['leiji_zhichu'],
            "curr_month_zhichu_jindu":obj['curr_month_zhichu_jindu'], "leiji_zhichu_jindu":obj['leiji_zhichu_jindu'],
            "yuji_zhichu":obj['yuji_zhichu'], "yuji_zhichu_jindu":obj['yuji_zhichu_jindu'],
            "sort_no": obj['sort_no']
                }
        
        SqlHelper.insert(sql9, param9)


if __name__ == '__main__':  
    initSqlHelper()

    print(f"开始执行统计数据计算： ")
    print(f"0 所有统计数据 ")

    for statistInfo in GlobalObject.statistArray:
        print(f"{statistInfo['numb']} {statistInfo['name']}")

    currYear = datetime.now().year 
    currMonth = datetime.now().month 

    if currMonth == 1:
        currMonth = 12
        currYear = currYear -1 
    else:
        currMonth -= 1

    numb1 = 0

    inputNumb = input(f"请选择你要刷新的统计接口的数据(默认 0)：")
    try:
        numb1 = int(inputNumb)
    except Exception as err2:
        print(f"输入的编号不合法，采用 {numb1}，即所有统计数据")
    

    minYear = 2000
    maxYear = 2300
    syear = currYear
    inputYear = input(f"请输入统计的年份(默认：{currYear})：")

    if inputYear != '':
        try:
            syear = int(inputYear)
        except Exception as err1:
            print(f"输入年份不合法，采用当前年份{currYear}")
            syear = currYear
        
    if syear<minYear or syear>maxYear:
        syear = currYear
        print(f"输入的年份数值不合理(要求 {minYear} < 年份 < {maxYear})，采用当前年份{currYear}")

    minMonth = 1
    maxMonth = 12
    smonth = currMonth
    inputMonth = input(f"请输入统计的月份(默认：{currMonth}):")
    if inputMonth != '':
        try:
            smonth = int(inputMonth)
        except Exception as err1:
            smonth=currMonth
            print(f"输入年份不合法，采用当前月份{currMonth}")
        
    if smonth<minMonth or smonth>maxMonth:
        smonth = currMonth
        print(f"输入的月份数值不合理(要求 {minMonth} < 月份 < {maxMonth})，采用当前月份{currMonth}")
    

    if numb1 == 0  or numb1 == 1:
        name1 = GlobalObject.getStatistName(1)
        print(f"== 开始统计 1 {name1} {syear}  {smonth}")
        refreshStatist01(syear,smonth)

    if numb1 == 0  or numb1 == 2:
        name1 = GlobalObject.getStatistName(2)
        print(f"== 开始统计 2 {name1} {syear}  {smonth}")
        refreshStatist02(syear)

    if numb1 == 0  or numb1 == 3:
        name1 = GlobalObject.getStatistName(3)
        print(f"== 开始统计 3 {name1} {syear}  {smonth}")
        refreshStatist03(syear)

    if numb1 == 0  or numb1 == 4:
        name1 = GlobalObject.getStatistName(4)
        print(f"== 开始统计 4 {name1} {syear}  {smonth}")
        refreshStatist04(syear)

    if numb1 == 0  or numb1 == 21:
        name1 = GlobalObject.getStatistName(21)
        print(f"== 开始统计 21 {name1} {syear}  {smonth}")
        print(f" ==== 处理 门诊的 ")
        refreshStatist21(1,syear,smonth)
        print(f" ==== 处理 住院的 ")
        refreshStatist21(2,syear,smonth)
    
    if numb1 == 0  or numb1 == 31:
        name1 = GlobalObject.getStatistName(31)
        print(f"== 开始统计 31 {name1} {syear}  {smonth}")
        print(f" ==== 处理 城市居民的 ")
        refreshStatist31('390',syear,smonth)
        print(f" ==== 处理 职工的 ")
        refreshStatist31('310',syear,smonth)

    if numb1 == 0  or numb1 == 33:
        name1 = GlobalObject.getStatistName(33)
        print(f"== 开始统计 33 {name1} {syear}  {smonth}")
        refreshStatist33(syear,smonth)

    if numb1 == 0  or numb1 == 34:
        name1 = GlobalObject.getStatistName(34)
        print(f"== 开始统计 34 {name1} {syear}  {smonth}")
        refreshStatist34(syear,smonth)

    if numb1 == 0  or numb1 == 35:
        name1 = GlobalObject.getStatistName(35)
        print(f"== 开始统计 35 {name1} {syear}  {smonth}")
        refreshStatist35(syear,smonth)

    if numb1 == 0  or numb1 == 41:
        name1 = GlobalObject.getStatistName(41)
        print(f"== 开始统计 41 {name1} {syear}  {smonth}")

        qyear = syear -1
        print(f" === 开始计算 {qyear} 年 {smonth} 月的  {name1}")
        refreshStatist41( qyear,smonth)
        print(f" === 开始计算 {syear} 年 {smonth} 月的  {name1}")
        refreshStatist41( syear,smonth)

    if numb1 == 0  or numb1 == 42:
        name1 = GlobalObject.getStatistName(42)
        print(f"== 开始统计 42 {name1} {syear}  {smonth}")

        qyear = syear -1
        print(f" === 开始计算 {qyear} 年 {smonth} 月的  {name1}")
        refreshStatist42( qyear,smonth)
        print(f" === 开始计算 {syear} 年 {smonth} 月的  {name1}")
        refreshStatist42( syear,smonth)

    if numb1 == 0  or numb1 == 51:
        name1 = GlobalObject.getStatistName(51)
        print(f"== 开始统计 51 {name1} {syear}  {smonth}")
        refreshStatist51( syear)


    if numb1 == 0  or numb1 == 1001:
        name1 = GlobalObject.getStatistName(1001)
        print(f"== 开始统计 1001 {name1} {syear} ")
        refreshMonthZongyusuan( syear)

